<?php 
/**
 * 订单控制器
 * @author 杨帆
 */
class OrderController extends CommonController{
    private  $model;
    
    public function __init(){
        $this->model = K('OrderBill');
    }
    /**
     * 默认显示
     */
    public function index(){
        //用户地址信息;
        if($_SESSION['uid']){
            $addData = K('address')->where("user_uid={$_SESSION['uid']}")->all();
            $this->assign('addData',$addData);
        }
        //城市地址
        $addressData = M('area')->where('parent_id=1')->all();
        $this->assign('addressData',$addressData); 
//      p($addressData);  
        //显示配送信息
        $deliveryData = K('Auxiliary')->where("pid=1")->all();
        $this->assign('deliveryData',$deliveryData);
        //显示付款信息
        $palData = K('Auxiliary')->where("pid=2")->all();
        $this->assign('palData',$palData);
        //显示商品信息
        $CartData = Cart::getGoods();
//      p($CartData);
        foreach ($CartData as $k => $v) {
            $CartData[$k]['options'] = implode(',', $v['options']);
            $sid[$k] =$k;
        }
//      p($sid);
//      p($CartData);
        $this->assign('CartData',$CartData);
        //商品总价格
        $totalPrice = Cart::getTotalPrice();
        $this->assign('totalPrice',$totalPrice);
//      p($totalPrice);
        //商品总件数
        $totalNum = Cart::getTotalNums();
        $this->assign('totalNum',$totalNum);
        //显示视图
        $this->display();
        
    }

    /**
      * ajax编辑地址
      */
//   public function ajaxaddr(){
//      
//      $adid = Q('post.id');
////        p($adid);
//
//      //获取原来地址信息
//      $oldData = K('Address')->where("adid={$adid}")->find();
////        p($oldData);
//      foreach ($oldData as $v) {
//          $oldData['add'] = explode('、', $oldData['address']);
//      }
//      
////        p($oldData);
//      $this->assign('oldData',$oldData);
//      $this->ajax($this->display(null,-1,null,'text/html',false));
//      
//   }
    
    /**
      * 添加地址
      */
    public function addRess(){
        //判断是不是ajax;
        if(!IS_AJAX) $this->error('页面不存在');
        //如果有数据传入
        if(IS_POST){
            //打开模型
            $model=K('Address');
            //如果客户输入的数据不合法,则返回错误
            if(!$model->_add()){
                $this->error($model->error);
            }
            //处理城市地址数据
            $province = Q('post.province',0,'intval');
            $province = M('area')->where("area_id={$province}")->getfield('area_name');
            $city = Q('post.city');
            $city = M('area')->where("area_id={$city}")->getfield('area_name');
            $county = Q('post.county');
            $county = M('area')->where("area_id={$county}")->getfield('area_name');
            //如果合法,则执行以下操作;
            $data = array(
               'consignee'=>Q('post.consignee'),
               'address'=>$province.'-'.$city.'-'.$county.'-'.Q('post.address').'-'.Q('post.zipCode'),
               'mobile'=>Q('post.mobile'),
               'user_uid' =>$_SESSION['uid'],
            );
            //添加成功并返回新增id;
            $addid = $model->add($data);
            //查询地址数据
            $addData = $model->where("addid={$addid}")->find();
            //将数据返回前台
            if($addData){
                echo json_encode($addData); exit;
            } else {
                echo 0; exit;
            }
        }
    }
    
    /**
      * 删除地址
      */
     public function del(){
        $addid = Q('post.id');
        $oldData = K('Address')->where("addid={$addid}")->delete();
        echo 1;die;
     }
    
    /**
     * 提交订单
     */
    public function addOrder(){
//      p($_POST);die;
        if(IS_POST){
            if(!$oid=$this->model->_addOrder()){
                $this->error($this->model->error);
            }
            //清空购物车
            cart::delAll();
            $this->success('订单提交成功','Index/Center/index');
        }
        
    }
    /**
     * 异步调取子地址
     */
    public function get_ress(){
        if(!IS_AJAX) $this->error('页面不存在');
        $pid = Q('post.pid',0,'intval');
        $addressData = M('area')->where("parent_id={$pid}")->all();
        if($addressData){
            echo json_encode($addressData); exit;
        } else {
            echo 0; exit;
        }
    }
 }
 
 
 
 
 
 
 
 
 
 
 


 ?>